<template>
  <div id="app">
    <div class="container">
      <div class="sidebar">
        <h2>消息中心</h2>
        <ul>
          <li><a href="#">我的消息</a></li>
          <li><a href="#">回复我的</a></li>
          <li><a href="#">@ 我的</a></li>
          <li><a href="#">收到的赞</a></li>
          <li><a href="#">系统通知</a></li>
        </ul>
      </div>
      <div class="main-content">
        <MessageList :messages="messages" />
        <ChatBox @send="handleSendMessage" />
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request';
import MessageList from './MessageList.vue';
import ChatBox from './ChatBox.vue';

export default {
  components: {
    MessageList,
    ChatBox,
  },
  data() {
    return {
      messages: [],
      chatMessage: '',
      offset: 0,
      more: true,
    };
  },
  created() {
    this.fetchMessages();
  },
  methods: {
    async fetchMessages() {
      try {
        const response = await request({
          url: '/msg/chat/recent-list',
          method: 'get',
          params: { offset: this.offset }
        });
        console.log('响应数据:', response); // 添加调试信息
        if (response && response.data) {
          const data = response.data;
          this.messages = data.list.map(item => ({
            id: item.chat.id,
            name: item.user.name,
            avatar: item.user.avatar,
            content: item.detail.content,
            latestTime: item.chat.latestTime,
            unread: item.chat.unread,
          }));
          this.more = data.more;
        } else {
          console.error('响应数据格式不正确:', response);
        }
      } catch (error) {
        console.error('获取消息失败:', error);
      }
    },
    handleSendMessage(message) {
      console.log('发送消息:', message);
      // 处理发送消息逻辑
    },
  },
};
</script>

<style>
body {
  font-family: Arial, sans-serif;
  background-color: #f5f5f5;
  margin: 0;
  padding: 0;
}

.container {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 200px;
  background-color: #e6f7ff;
  padding: 15px;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
}

.sidebar h2 {
  margin-top: 0;
  font-size: 18px;
}

.sidebar ul {
  list-style: none;
  padding: 0;
}

.sidebar ul li {
  margin: 8px 0;
}

.sidebar ul li a {
  text-decoration: none;
  color: #333;
  font-size: 14px;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 15px;
}

.message-list {
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  flex: 1;
}

.message-list .message-item {
  display: flex;
  padding: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.message-list .message-item:last-child {
  border-bottom: none;
}

.message-list .message-item img {
  border-radius: 50%;
  margin-right: 10px;
  width: 40px;
  height: 40px;
}

.message-list .message-item .message-info {
  flex: 1;
}

.message-list .message-item .message-info h4 {
  margin: 0;
  font-size: 14px;
}

.message-list .message-item .message-info p {
  margin: 5px 0 0;
  color: #666;
  font-size: 12px;
}

.message-detail h3 {
  margin-top: 0;
  font-size: 16px;
}

.message-detail p {
  color: #666;
  font-size: 14px;
}

.chat-box {
  display: flex;
  padding: 10px;
  background-color: #fff;
  border-top: 1px solid #f0f0f0;
  box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
  margin-top: 15px;
}

.chat-box input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 5px;
  margin-right: 10px;
  font-size: 14px;
}

.chat-box button {
  padding: 8px 15px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
}

.chat-box button:hover {
  background-color: #0056b3;
}
</style>
